package com.ruoyi.map.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.map.domain.BuildingInfo;
import com.ruoyi.map.domain.RegionInfo;
import com.ruoyi.map.mapper.BuildingInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.map.mapper.UnitInfoMapper;
import com.ruoyi.map.domain.UnitInfo;
import com.ruoyi.map.service.IUnitInfoService;

import javax.annotation.Resource;

/**
 * 保障房建筑单元信息数据Service业务层处理
 *
 * @author dihang
 * @date 2023-07-26
 */
@Service
public class UnitInfoServiceImpl extends ServiceImpl<UnitInfoMapper, UnitInfo> implements IUnitInfoService
{
    @Resource
    UnitInfoMapper unitInfoMapper;
    @Resource
    BuildingInfoMapper buildingInfoMapper;


    /**
     * 查询保障房建筑单元信息数据列表
     *
     *
     * @param
     * @return 保障房建筑单元信息数据
     */
    @Override
    public List<UnitInfo> selectUnitInfoList()
    {
        List<UnitInfo> unitInfos = unitInfoMapper.selectList(null);
        return unitInfos;
    }


    /**
     * 新增保障房建筑单元信息数据列表
     * @param unitInfo
     */
    @Override
    public void addNew(UnitInfo unitInfo) {
        if (unitInfo.getId()==null){
            throw new BaseException("主键ID不能为空！");
        }
        LambdaQueryWrapper<UnitInfo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UnitInfo::getName,unitInfo.getName());
        queryWrapper.eq(UnitInfo::getDeleted,"0");////deleted为是否删除 0 否 1 是
        Long selectCount = unitInfoMapper.selectCount(queryWrapper);
        if (selectCount>0){
            throw new BaseException("该单元已存在，不能重复录入！");
        }
        //传入参数时，根据填入的所属楼栋来确定楼栋的主键ID
        String affiliationBuilding = unitInfo.getAffiliationBuilding();
        BuildingInfo buildingInfo = buildingInfoMapper.selectOne
                (Wrappers.<BuildingInfo>lambdaQuery().eq(BuildingInfo::getName, affiliationBuilding));
        if (buildingInfo==null){
            throw new BaseException("数据库中没有您填写的所属区域！");
        }
        Long id = buildingInfo.getId();
        unitInfo.setBuildingId(id);
        this.save(unitInfo);

    }


    /**
     * 分页-条件查询保障房建筑单元信息数据列表
     * @param unitInfo
     * @return
     */
    @Override
    public IPage<UnitInfo> unitSearch(UnitInfo unitInfo) {
        IPage<UnitInfo>page=this.page(new Page<>(unitInfo.getPageNum(),unitInfo.getPageSize()),
                oneSelectWhere(unitInfo));
        return page;
    }

    /**
     * 条件查询 封装QueryWrapper
     *
     * @param unitInfo
     */
    private LambdaQueryWrapper<UnitInfo> oneSelectWhere(UnitInfo unitInfo) {
        LambdaQueryWrapper<UnitInfo>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (unitInfo==null){
            return lambdaQueryWrapper;
        }
        lambdaQueryWrapper.like(StrUtil.isNotBlank(unitInfo.getName()),UnitInfo::getName,unitInfo.getName())
                .like(StrUtil.isNotBlank(unitInfo.getAffiliationBuilding()),UnitInfo::getAffiliationBuilding,unitInfo.getAffiliationBuilding())
                .eq(UnitInfo::getDeleted,"0");//是否删除 0否 1是
        lambdaQueryWrapper.orderByDesc(UnitInfo::getCreateTime);
        return lambdaQueryWrapper;
    }

}
